var App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize:10,
    list:[],
    page:1,// 当前页码
    last_page:1,
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    isScrollAble:true,
    refreshOpen:true, //下拉刷新/下拉复位
    refreshstatus:true, //是否开启下拉刷新
    // 
    showPopup:false,
    off_status:1,
    off_title:'请输入车牌号',
    car_number:0,
    keyWords:'',
    // 
    car_code:'',
    phone:'',
    car_id:'',
  },

  onLoad(options) {
    this.setListHeight()
    this.getDataList();
  },
  onReady() {},
  onShow() {},
  // 弹窗
  onShowPopup(e){
    let that = this;
    let car_code = '',  phone ='', car_id = '';
    if(that.data.showPopup == false){
      car_code = e.currentTarget.dataset.code;
      phone = e.currentTarget.dataset.phone;
      car_id = e.currentTarget.dataset.id;
    }
    that.setData({
      showPopup: !that.data.showPopup,
      car_code,
      phone,
      car_id
    }); 
  },
  // 单选
  radioChange(e){
    console.log(e.detail.value)
    this.setData({
      off_status:e.detail.value,
      off_title:e.detail.value==1?'请输入车牌号':(e.detail.value==2?'请输入手机号':'请输入用户ID')
    })
  },
  bindinput(e){
    this.setData({keyWords:e.detail.value})
  },
  bindinput1(e){
    this.setData({car_code:e.detail.value})
  }, 
  //提交
  formSubmit(e){
    let that =  this
    let obj = {
      car_id:that.data.car_id,
      phone:that.data.phone,
      car_code:that.data.car_code
    }
    App.request('/Manage/Management/changeCars',obj,function(e){
        if (e.code == "200") {
          App.showSuccessToast(e.msg, function() {
            that.getDataList()          //刷新
            that.onShowPopup()
          });
        } else{
          App.showError(e.msg);
        }
    })
  },
  //获取数据列表
  getDataList(isPage, page){
    let that = this;
    let obj = {
      b_id:App.get_b_id(),
      page: page || 1,
      pageSize:that.data.pageSize,
      type:that.data.off_status,
      keyWords:that.data.keyWords,
    }
    App.request('/Manage/Management/manageCars',obj,function(e){
       // 刷新状态复位
       that.setData({
        refreshOpen: 0 
      });
        // 不加这个方法真机下拉会一直处于刷新状态，无法复位
        if (e.code == "200") {
          let resList = e.result,dataList = that.data.list;
          if (isPage == true) {
            that.setData({
              list:dataList.concat(resList.data),
              isLoading: false,
              last_page: resList.total,
              car_number: resList.cars_num,
            })
          }else{
            that.setData({
              list: resList.data,
              isLoading: false,
              last_page: resList.total,
              car_number: resList.cars_num,
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000
          })
        }
    })
  },
  // 自定义下拉刷新
  upRefresh(){
    this.setData({
      list:[],
      page:1,// 当前页码
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    })
    this.getDataList()
  },
  /**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    // 已经是最后一页
    console.log("翻页");
    console.log("page =",this.data.page)
    console.log("last_page =",this.data.last_page)
    if (this.data.page > this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },
  /**
   * 设置文章列表高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 160), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
      console.log(systemInfo.windowHeight,scrollHeight);
      this.setData({
        scrollHeight
      });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})